package com.zx.viewlearn.views.pullableview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;

/**
 * Author     Sdz
 * Email      946616236@qq.com
 * Time       2016/11/11 0011
 * Function   可上拉下拉刷新的RecyclerView
 */

public class PullableRecyclerView extends RecyclerView implements Pullable {

    public static final int TYPE_LIN = 0;

    public static final int TYPE_GRID = 1;

    public static final int TYPE_STAG = 2;
    /**
     * RecyclerView的manager类型
     * 0：默认LinearLayoutManager
     * 1：网格布局GridLayoutManager
     * 2：瀑布布局StaggeredGridLayoutManager
     */
    private int managerType = 0;
    //是否可以上拉
    private boolean canPullUp = true;
    //是否可以下拉
    private boolean canPullDown = true;
    //列表最后项
    private int lastItemPosition;
    //列表可见项
    private int firstItemPosition;
    //布局管理
    private LayoutManager layoutManager;

    // 用来记录lastItem的position
    // 由于瀑布流有多个列 所以此处用数组存储
    int column;
    int positions[];

    public PullableRecyclerView(Context context) {
        super(context);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullableRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setManagerType(int managerType) {
        this.managerType = managerType;
    }

    public void setCanPullDown(boolean canPullDown) {
        this.canPullDown = canPullDown;
    }

    public void setCanPullUp(boolean canPullUp) {
        this.canPullUp = canPullUp;
    }

    @Override
    public boolean canPullDown() {
        getManager();
        // 没有item的时候也可以下拉刷新
        if (getAdapter() == null && canPullDown) {
            return true;
        }
        return (getAdapter().getItemCount() == 0 || firstItemPosition == 0 && getChildAt(0).getTop() >= 0) && canPullDown;
    }

    @Override
    public boolean canPullUp() {
        getManager();
        if (getAdapter() == null && canPullUp) {
            return true;
        }
        if (getAdapter().getItemCount() == 0 && canPullUp) {
            // 没有item的时候也可以上拉加载
            return true;
        } else if (lastItemPosition == (getAdapter().getItemCount() - 1) && managerType != TYPE_STAG) {
            // 滑到底部了
            if (getChildAt(lastItemPosition - firstItemPosition) != null
                    && getChildAt(lastItemPosition - firstItemPosition).getBottom() <= getMeasuredHeight() && canPullUp)
                return true;
        } else if (managerType == TYPE_STAG) {
            for (int i = 0; i < positions.length; i++) {
                if (positions[i] >= (getAdapter().getItemCount() - column)
                        && getLayoutManager().findViewByPosition(positions[i]).getBottom() <= getHeight()) {
                    return true;
                }
            }
        }
        return false;
    }

    private void getManager() {
        if (layoutManager == null) {
            layoutManager = getLayoutManager();
        }

        if (managerType == TYPE_LIN) {
            //判断是当前layoutManager是否为LinearLayoutManager
            // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
            if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                //获取最后一个可见view的位置
                lastItemPosition = linearManager.findLastVisibleItemPosition();
                //获取第一个可见view的位置
                firstItemPosition = linearManager.findFirstVisibleItemPosition();
            }
        } else if (managerType == TYPE_GRID) {
            //网格布局GridLayoutManager
            if (layoutManager instanceof GridLayoutManager) {
                GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                lastItemPosition = gridLayoutManager.findLastVisibleItemPosition();
                firstItemPosition = gridLayoutManager.findFirstVisibleItemPosition();
            }
        } else if (managerType == TYPE_STAG) {
            //瀑布布局StaggeredGridLayoutManager
            if (layoutManager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                // 用来记录lastItem的position
                // 由于瀑布流有多个列 所以此处用数组存储
                column = staggeredGridLayoutManager.getColumnCountForAccessibility(null, null);
                positions = new int[column];
                // 获取lastItem的positions
                staggeredGridLayoutManager.findLastVisibleItemPositions(positions);
//                for (int i = 0; i < positions.length; i++) {
//                    /**
//                     * 判断lastItem的底边到recyclerView顶部的距离
//                     * 是否小于recyclerView的高度
//                     * 如果小于或等于 说明滚动到了底部
//                     */
//                    // 刚才忘了写判断是否是最后一个item了
//                    if (positions[i] >= (staggeredGridLayoutManager.getItemCount() - column)
//                            && staggeredGridLayoutManager.findViewByPosition(positions[i]).getBottom() <= getHeight()) {
//                        Log.d("sssss", "getManager: ");
//                        /**
//                         * 此处实现你的业务逻辑
//                         */
//                        break;
//                    }
//
//                }
            }
        }
    }
}
